package com.molichuxing.baselibrary.views;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.InputType;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.molichuxing.baselibrary.R;
import com.molichuxing.baselibrary.utils.LogUtils;

import java.lang.reflect.Method;

public class CustomNumKayUtils implements CustomNumKeyView.CallBack{
    private CustomNumKeyView my_key;

    private View my_root;
    private EditText et_view;

    public CustomNumKayUtils(EditText met_view) {
        this.et_view = met_view;
    }

    public void initKeyBoad(final Context mContext,
                            CustomNumKeyView m_key, View m_root) {
        my_key = m_key;
        my_root = m_root;
        // 自己监听EditText的点击事件弹出我们自定义的键盘
        et_view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                show(mContext);
                return false;
            }
        });
        et_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                et_view.setSelection(et_view.length());//将光标移至文字末尾
            }
        });
        my_root.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                hide();
                return false;
            }
        });
        // 设置回调，并进行文本的插入与删除
        my_key.setOnCallBack(this);
        my_key.setRandomKeyBoard(true);
    }

    /**
     * 多处控件调用
     * @param m_key
     */
    public void initKeyBoad(CustomNumKeyView m_key, View m_root) {
        my_key = m_key;
        my_root = m_root;
        // 设置回调，并进行文本的插入与删除
        my_key.setOnCallBack(this);
        my_key.setRandomKeyBoard(true);
    }

    public void show(Context mContext){
        if(mContext==null||et_view==null||my_key==null||my_root==null){
            return;
        }

        // 设置不弹出系统键盘
        disableShowSoftInput(et_view);
        et_view.setSelection(et_view.length());//将光标移至文字末尾

        InputMethodManager imm = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_view.getWindowToken(),0);

        et_view.setFocusable(true);
        et_view.setFocusableInTouchMode(true);
        et_view.requestFocus();
        my_key.setVisibility(View.VISIBLE);

        InputMethodManager imm_root = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm_root.hideSoftInputFromWindow(my_root.getWindowToken(), 0);
    }

    public void show(Context mContext,EditText met_view){
        this.et_view = met_view;
        if(mContext==null||et_view==null||my_key==null||my_root==null){
            return;
        }

        // 设置不弹出系统键盘
        disableShowSoftInput(et_view);
        et_view.setSelection(et_view.length());//将光标移至文字末尾

        InputMethodManager imm = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(et_view.getWindowToken(),0);

        et_view.setFocusable(true);
        et_view.setFocusableInTouchMode(true);
        et_view.requestFocus();
        my_key.setVisibility(View.VISIBLE);

        InputMethodManager imm_root = (InputMethodManager)
                mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm_root.hideSoftInputFromWindow(my_root.getWindowToken(), 0);
    }

    public void hide(){
        if(my_key!=null){
            my_key.setVisibility(View.GONE);
        }
    }

    @Override
    public void clickNum(String num) {
        if (et_view.getText().length() < 8) {
            et_view.setSelection(et_view.length());//将光标移至文字末尾
            et_view.append(num);
            et_view.setTransformationMethod(PasswordTransformationMethod.getInstance());
            //文本长度为8时隐藏键盘
//            if (et_view.getText().length() == 8) {
//                hide();
//            }
        }
    }

    @Override
    public void deleteNum() {
        int last = et_view.getText().length();
        if (last > 0) {
            et_view.setSelection(et_view.length());//将光标移至文字末尾
            //删除最后一位
            et_view.getText().delete(last - 1, last);
        }
    }

    @Override
    public void hideNum() {
        hide();
    }

    public void disableShowSoftInput(EditText editText) {
        if (android.os.Build.VERSION.SDK_INT <= 10) {
            editText.setInputType(InputType.TYPE_NULL);
        } else {
            Class<EditText> cls = EditText.class;
            Method method;
            try {
                method = cls.getMethod("setShowSoftInputOnFocus", boolean.class);
                method.setAccessible(true);
                method.invoke(editText, false);
            } catch (Exception e) {
            }
        }
    }

    public EditText getEt_view() {
        return et_view;
    }

    public void setEt_view(EditText et_view) {
        this.et_view = et_view;
    }
}
